package com.xiaoming;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.InputStream;

/***
 * 根据InputStream封装Request
 */
@Data
@NoArgsConstructor
public class Request {
    private String method;
    private String url;
    // 输入流，用于解析 url 和 method
    private InputStream inputStream;

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputStr = new String(bytes);
        // 获取请求头
        String firstLine = inputStr.split("\\n")[0];
        String[] strings = firstLine.split(" ");
        this.method = strings[0];
        this.url = strings[1];
        System.out.println("method：" + method + " url：" + url);
    }
}
